﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.Utility;

namespace OgilvyOne.SqlServerDAL
{
    public class CmsUserSession : ICmsUserSession
    {
        public bool Insert(CmsUserSessionModel session)
        {
            bool re = false;
            string insertsql = "insert into tbl_site_session(col_user_id,col_client_ip,col_login_time) "
                + "values(" + session.UserID + ",@clientIP,getDate())";

            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@clientIP", SqlDbType.NVarChar, 50)
            };

            parms[0].Value = session.ClientIP;
            //parms[1].Value = session.ClientMac;

            int i = SQLHelper.ExecuteSql (insertsql, parms);
            if (i > 0)
                re = true;

            return re;
        }

        private const string GET_SESSION_SQL = "select col_user_id, col_client_ip,  col_login_time from tbl_site_session where col_client_ip=@clientIP";
        public CmsUserSessionModel GetUserSessionModel(string clientIP/*, string clientMac*/)
        {
            if (clientIP == null) throw new ArgumentNullException("clientIP");
            //if (clientMac == null) throw new ArgumentNullException("clientMac");

            CmsUserSessionModel model = null;
            try
            {
                SqlParameter[] parms = new SqlParameter[] {
                new SqlParameter("@clientIP", SqlDbType.NVarChar, 50)
                //new SqlParameter("@clientMac", SqlDbType.NVarChar, 50)
                };
                parms[0].Value = clientIP;
                //parms[1].Value = clientMac;

                using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, GET_SESSION_SQL, parms))
                {
                    if (rdr.Read())
                    {
                        model = new CmsUserSessionModel();
                        model.UserID = rdr.GetInt32(0);
                        model.ClientIP = rdr.GetString(1);
                        //model.ClientMac = rdr.GetString(2);
                        if (!rdr.IsDBNull(2)) model.LoginTime = rdr.GetDateTime(2);
                    }
                }

                return model;
            }
            catch (Exception ex)
            {
                throw new Exception("Error occurred in CmsUserSession.GetUserSessionModel. Message: " + ex.Message);
            }
        }

        public bool Delete (string clientIP)
        {
            bool re = false;
            string delsql = string.Format ("delete from tbl_site_session where col_client_ip='{0}'", clientIP);

            int i = SQLHelper.ExecuteSql (delsql, null);
            if (i > 0) re = true;
            return re;
        }

        public bool UpdateLoginTime(string clientIP)
        {
            bool re = false;
            string sql = string.Format("update tbl_site_session set col_login_time=getdate() where col_client_ip='{0}'", clientIP);
            try
            {
                int i = SQLHelper.ExecuteSql(sql);
                if (i > 0) re = true;
            }
            catch (Exception ex)
            {
                re = false;
            }

            return re;
        }
    }
}
